<template>
  <div>
    <h1>Users</h1>
    <el-container>
      <el-header>
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
          <el-form-item label="用户名称：" prop="username">
            <el-input v-model="ruleForm.username"  clearable ></el-input>
          </el-form-item>

          <el-form-item label="用户密码：" prop="password">
            <el-input v-model="ruleForm.password"  clearable ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')">立即登录</el-button>
            <el-button @click="resetForm('ruleForm')">重置</el-button>
          </el-form-item>
        </el-form>
      </el-header>
      <el-main>Main</el-main>
    </el-container>
  </div>
</template>

<script>
export default {
  name: "login",

  data() {
    return {
      ruleForm: {
        username: "",
        password: "",
      },
      rules: {
        username: [
          { required: true, message: "必填" },
          { min: 1, max: 18, message: '长度在 6 到 18 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: "必填" },
          { min: 6, max: 18, message: '长度在 6 到 18 个字符', trigger: 'blur' }
        ]
      }
    };
  },
  methods: {
    submitForm(formName) {
      let _this = this;
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$http.post("/auth/login", this.ruleForm).then(function(response) {
            console.log(response.data);
            _this.$router.push("/index");
          }).catch(function(response) {
            console.log(response);
          });
        } else {
          console.log('error submit!!');
          return false;
        }


      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>

<style scoped>

</style>
